﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="FormPage.aspx.cs" Inherits="MO.BusinessServer.Web.FormPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderMain" runat="server">
    <script>
        var formCode = "<%= this.FormCode %>";
        var dataCode = "<%= this.DataCode %>";
    </script>
    <form id="main_form">
        <ul data-role="listview" data-inset="true" id="formbody">
        </ul>
    </form>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderFooter" runat="server">
    <div data-role="navbar" data-iconpos="left">
        <ul>
            <li><a href="/" data-icon="back">返回</a></li>
            <li><a id="form_submit" href="#" data-icon="check" class="ui-btn-active">提交</a></li>
        </ul>
    </div>


    <script>

        // 等待PhoneGap连接设备
        document.addEventListener("deviceready", function () { }, false);

        $(function () {
            var form = {};
            var formManager = Proxy.GetInstance("MO.BusinessServer.FormManager");
            if (dataCode != "")
                form = formManager.Invoke("GetForm", [formCode, dataCode]);
            else
                form = formManager.Invoke("GetForm", [formCode]);
            $("#title_h1").text(form.Name);
            $("title").text(form.Name);
            var container = $("#formbody");
            for (var i = 0; i < form.Items.length; i++) {
                var item = form.Items[i];
                var li = $("<li />").attr("Code", item.Code);
                container.append(li);

                switch (item.ControlType) {
                    case "text": {
                        var label = $("<label />").attr("for", item.Code).text(item.Name + ":");
                        var input = $("<input />").attr({ type: "text", value: item.DefaultValue, name: item.Code, id: item.Code });
                        li.append(label).append(input);
                        break;
                    }
                    case "textarea": {
                        var label = $("<label />").attr("for", item.Code).text(item.Name + ":");
                        var input = $("<textarea />").attr({ rows: "4", name: item.Code, id: item.Code }).text(item.DefaultValue);
                        li.append(label).append(input);
                        break;
                    }
                    case "radio": {
                        var fieldset = $("<fieldset data-role='controlgroup' />").append($("<legend>" + item.Name + ":</legend>"));
                        for (var j = 0; j < item.ControlData.length; j++) {
                            var ds = item.ControlData[j];
                            if (ds[0] == item.DefaultValue)
                                var input = $("<input />").attr({ type: "radio", value: ds[0], name: item.Code, id: item.Code + j, checked: "checked" });
                            else
                                var input = $("<input />").attr({ type: "radio", value: ds[0], name: item.Code, id: item.Code + j });
                            var label = $("<label />").attr("for", item.Code + j).text(ds[1]);
                            fieldset.append(input).append(label);
                        }
                        li.append(fieldset);
                        break;
                    }
                    case "checkbox": {
                        var fieldset = $("<fieldset data-role='controlgroup' />").append($("<legend>" + item.Name + ":</legend>"));
                        for (var j = 0; j < item.ControlData.length; j++) {
                            var ds = item.ControlData[j];
                            var input = $("<input />").attr({ type: "checkbox", value: ds[0], name: item.Code, id: item.Code + j });
                            for (var k = 0; k < item.DefaultValue.length; k++) {
                                if (ds[0] == item.DefaultValue[k])
                                    var input = $("<input />").attr({ type: "checkbox", value: ds[0], name: item.Code, id: item.Code + j, checked: "checked" });
                            }
                            var label = $("<label />").attr("for", item.Code + j).text(ds[1]);
                            fieldset.append(input).append(label);
                        }
                        li.append(fieldset);
                        break;
                    }
                    case "select": {
                        var label = $("<label class='select' />").attr("for", item.Code).text(item.Name + ":");
                        var select = $("<select />").attr({ name: item.Code, id: item.Code });;
                        for (var j = 0; j < item.ControlData.length; j++) {
                            var ds = item.ControlData[j];
                            if (ds[0] == item.DefaultValue)
                                var option = $("<option />").attr({ value: ds[0], selected: "selected" }).text(ds[1]);
                            else
                                var option = $("<option />").attr({ value: ds[0] }).text(ds[1]);
                            select.append(option);
                        }
                        li.append(label).append(select);
                        break;
                    }
                    case "number": {
                        var label = $("<label />").attr("for", item.Code).text(item.Name + ":");
                        var input = $("<input />").attr({ type: "number", value: item.DefaultValue, pattern: "[0-9]*", name: item.Code, id: item.Code });
                        li.append(label).append(input);
                        break;
                    }
                    case "bool": {
                        var label = $("<label />").attr("for", item.Code).text(item.Name + ":");
                        var select = $("<select data-role='slider' data-mini='true' />").attr({ name: item.Code, id: item.Code });;
                        for (var j = 0; j < item.ControlData.length; j++) {
                            var ds = item.ControlData[j];
                            if (ds[0] == item.DefaultValue)
                                var option = $("<option />").attr({ value: ds[0], selected: "selected" }).text(ds[1]);
                            else
                                var option = $("<option />").attr({ value: ds[0] }).text(ds[1]);
                            select.append(option);
                        }
                        li.append(label).append(select);
                        break;
                    }
                    case "time": {
                        break;
                    }
                    case "date": {
                        var label = $("<label />").attr("for", item.Code).text(item.Name + ":");
                        var input = $("<input />").attr({ type: "date", name: item.Code, id: item.Code, value: item.DefaultValue });
                        li.append(label).append(input);
                        break;
                    }
                    case "img": {
                        var label = $("<label />").attr("for", item.Code).text(item.Name + ":");
                        var button = $("<a href='#' class='ui-btn'>拍照</a>");
                        var input = $("<input />").attr({ type: "hidden", name: item.Code });
                        var img = $("<img />").width("200px").height("200px");
                        
                        button.click(this, function () {
                            navigator.camera.getPicture(function (imageData) {
                                img.attr("src", "data:image/jpeg;base64," + imageData);
                                input.val(imageData);
                                label.after(input);
                                label.after(img);
                            },
                            function (mesage) {
                                alert('Failed because: ' + message);
                            },
                            {
                                quality: 50,
                                destinationType: navigator.camera.DestinationType.DATA_URL
                            });
                        });
                        li.append(label).append(button);
                        if (item.DefaultValue) {
                            li.append(img);
                            img.attr("src", "/Upload/" + item.DefaultValue);
                        }
                        break;
                    }
                    case "sound": {
                        break;
                    }
                    case "label": {
                        var label = $("<label />").text(item.Name + ":");
                        var content = $("<h2 />").text(item.DefaultValue);
                        li.append(label).append(content);
                        break;
                    }
                    case "hide": {
                        var input = $("<input />").attr({ type: "hidden", value: item.DefaultValue, name: item.Code, id: item.Code });
                        li.append(input);
                        li.hide();
                        break;
                    }

                }

            }
            container.trigger('create');


            $("#form_submit").click(this, function () {
                var inputDict = $("#main_form").serializeJson();// 取数据
                for (var i = 0; i < form.Items.length; i++) {
                    var item = form.Items[i];
                    if (item.CheckCondition != "") {
                        var checks = eval(item.CheckCondition);// 检查
                        var value = inputDict[item.Code];
                        for (var j = 0; j < checks.length; j++) {
                            if (!eval(checks[j][0])) {
                                PromptMessage(checks[j][1]); // 提示错误
                                return;
                            }
                        }
                    }
                }
                // 提交
                formManager.Invoke("SubmitForm", [formCode, inputDict], function (result) {
                    PromptMessage("提交成功");
                    setTimeout(function () { location.href = "/"; }, 1000);
                });
            });
            function PromptMessage(mes) {
                $("<div><p>" + mes + "</p></div>").popup().popup("open");
            }
        });
    </script>
</asp:Content>
